﻿package kliment.data {
	import flash.display.InteractiveObject;
	import flash.events.FocusEvent;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	import kliment.BaseApplication;
	import kliment.controls.ComboBox;
	
	/**
	 * ...
	 * @author	Kliment
	 * @version 1.1.5
	 * 
	 * class name: kliment.data.FormField
	 */
	public class FormField {
		private static var _selectedTextField:TextField;
		private var _target:InteractiveObject;
		private var _required:Boolean;
		
		public var start_value:*;
		public var form:Form;
		public var name:String;
	
		public function FormField(target:InteractiveObject, name:String, start_value:* = null, required:Boolean = false) {
			_target = target;
			this.start_value = start_value;
			_required = required;
			this.name = name;
			_setActions();
			if (start_value != null) {
				value = start_value;
			}
		}
		
		private function _setActions():void {
			if (_target is TextField) {
				var textField:TextField = _target as TextField;
				textField.addEventListener(FocusEvent.FOCUS_IN, _textFieldHandler);
				textField.addEventListener(FocusEvent.FOCUS_OUT, _textFieldHandler);
			}
		}
		
		private function _textFieldHandler(event:FocusEvent):void {
			var textField:TextField = _target as TextField;
			switch(event.type) {
				case FocusEvent.FOCUS_IN:
					if (textField.text == String(start_value))
						textField.text = '';
					_selectedTextField = textField;
					if (BaseApplication.fullScreen) 
						setTimeout(_toNormalScreen, 10);
				break;
				
				case FocusEvent.FOCUS_OUT:
					if (textField.text == '')
						textField.text = String(start_value);
				break;
			}
		}
		
		private function _toNormalScreen():void {
			BaseApplication.fullScreen = false;
			if (_selectedTextField.stage)
				_selectedTextField.stage.focus = _selectedTextField;
		}
		
		public function get isValid():Boolean {
			if (_required && value == '')
				return false;
			if (value == start_value)
				return false;
			return true;
		}
		
		public function get value():* {
			var val:*;
			if (_target is TextField) {
				var textField:TextField = _target as TextField;
				val = textField.text;
			/*} else if (_target instanceof CheckBox) {
				var checkBox:CheckBox = _target;
				val = checkBox.value;*/
			} else if (_target is ComboBox) {
				var comboBox:ComboBox = _target as ComboBox;
				val = comboBox.value;
			}
			return val;
		}
		
		public function set value(value:*):void {
			if (_target is TextField) {
				var textField:TextField = _target as TextField;
				textField.text = String(value);
			} /*else if (_target instanceof CheckBox) {
				var checkBox:CheckBox = _target;
				checkBox.value = Boolean(value);
			}*/
		}
		
		public function clear():void {
			value = start_value;
		}
		
		public function remove():void {
			clear();
			if (_target is TextField) {
				var textField:TextField = _target as TextField;
				textField.removeEventListener(FocusEvent.FOCUS_IN, _textFieldHandler);
				textField.removeEventListener(FocusEvent.FOCUS_OUT, _textFieldHandler);
			}
		}
	}

}